package com.shujia.flink.window;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo2CountWindow {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> linesDS = env.socketTextStream("master", 8888);

        /**
         * 实时统计单词的数量，每10条数据统计一次
         *  .countWindow(10): 滚动的统计窗口
         *  .countWindow(10，2)：滑动的统计窗口
         */
        linesDS.map(word -> Tuple2.of(word, 1), Types.TUPLE(Types.STRING, Types.INT))
                .keyBy(kv -> kv.f0)
                //每隔10条数据计算一次,同一个key每隔10条计算一次
                .countWindow(10)
                .sum(1)
                .print();

        env.execute();


    }
}
